package com.ddwl.order.service;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.OrderEnum;
import com.ddwl.common.feign.api.GoodsApi;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.order.dao.OrderPhaseDao;
import com.ddwl.order.dao.OrderSpuDao;
import com.ddwl.order.dao.model.OrderInfo;
import com.ddwl.order.dao.model.OrderPhase;
import com.ddwl.order.dao.model.OrderSpu;
import com.ddwl.order.service.dto.CreateOrderDto;
import com.ddwl.order.service.dto.OrderSkuListDto;
import com.ddwl.schema.bo.distr.ComputeProfitPriceBo;
import com.ddwl.schema.vo.dist.ComputeProfitPriceVo;
import com.ddwl.schema.vo.order.OrderSpuApiVo;
import com.ddwl.schema.vo.order.OrderSpuVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * 类OrderSpuServer.java的实现描述：订单商品处理
 *
 * @author zach
 * @Date :2020-06-02 14:18.
 */
@Service
public class OrderSpuServer {

    @Resource
    private OrderSpuDao orderSpuDao;
    @Resource
    private OrderPhaseDao orderPhaseDao;
    @Resource
    private GoodsApi goodsApi;


    /**
     * 查询订单发生售后的商品
     * @param orderId
     * @return
     */
    public List<OrderSpuVo> getOrderAfterSaleSpu(String orderId){
        List<OrderSpu> orderSpus = orderSpuDao.getOrderAfterSaleSpu(orderId);
        return BeanConverUtils.convertBeanList(orderSpus,OrderSpuVo.class);
    }

    /**
     * 查询订单商品信息
     * @param orderId
     * @return
     */
    public List<OrderSpuVo> getOrderSpu(String orderId) {
        OrderSpu spu = new OrderSpu();
        spu.setOrderId(orderId);
        return BeanConverUtils.convertBeanList(orderSpuDao.select(spu), OrderSpuVo.class);
    }
    /**
     * 查询订单商品信息
     * @param spu
     * @return
     */
    public List<OrderSpuVo> getOrderSpu(OrderInfo spu) {
        return BeanConverUtils.convertBeanList(orderSpuDao.getOrderSpu(spu), OrderSpuVo.class);
    }

    /**
     * 查询订单商品信息
     * @param orderId
     * @return
     */
    public List<OrderSpuApiVo> getOrderApiSpu(String orderId) {
        OrderSpu spu = new OrderSpu();
        spu.setOrderId(orderId);
        return BeanConverUtils.convertBeanList(orderSpuDao.select(spu), OrderSpuApiVo.class);
    }
    /**
     * 创建订单商品信息
     * @param orderInfo
     * @param createOrderDto
     * @return
     */
    public List<OrderSpu> createOrderSpu(OrderInfo orderInfo, CreateOrderDto createOrderDto) {
        List<OrderSpu> spus = new ArrayList<>();
        //计算商品津贴后的最终单价
        for (OrderSkuListDto sku : createOrderDto.getSkuLists()) {
            //用商品优惠后的价格乘以件数得出优惠后的总价,然后扣除津贴金额,最后除以件数得出商品津贴后的单价
            BigDecimal skuSubAvg = sku.getRealPrice().multiply(BigDecimal.valueOf(sku.getSkuQty())).subtract(sku.getSubsidyPrice()).divide(BigDecimal.valueOf(sku.getSkuQty()), 2, RoundingMode.HALF_UP);
            /**
             * 核账操作补余
             * 用户商品优惠后的价格乘以件数 减去商品津贴后的单价乘以件数+商品津贴金额的差值 为系统误差
             */
            BigDecimal total =  sku.getRealPrice().multiply(BigDecimal.valueOf(sku.getSkuQty()));
            BigDecimal realTotal =  skuSubAvg.multiply(BigDecimal.valueOf(sku.getSkuQty()));
            sku.setOtherSubsidyPrice(total.subtract(realTotal.add(sku.getSubsidyPrice())));
            sku.setRealPrice(skuSubAvg);
        }


        //计算佣金
        List<ComputeProfitPriceBo> itemBos = new ArrayList<>();
        createOrderDto.getSkuLists().stream().forEach(o->{
            ComputeProfitPriceBo bo=new ComputeProfitPriceBo();
            bo.setCategoryId(o.getCategoryId());
            bo.setItemId(o.getItemId());
            bo.setPrice(o.getRealPrice());
            bo.setSkuId(o.getSkuId());
            itemBos.add(bo);
        });
        List<ComputeProfitPriceVo> result = goodsApi.computeProfitPrice(itemBos).getResult();
        for (OrderSkuListDto sku : createOrderDto.getSkuLists()) {
            for (ComputeProfitPriceVo profitPriceVo:result) {
                if (sku.getSkuId().equals(profitPriceVo.getSkuId())){
                    sku.setProfitPrice(profitPriceVo.getProfitPrice());
                }
            }
        }


        for (OrderSkuListDto skuList : createOrderDto.getSkuLists()) {
            OrderSpu orderSpu = BeanConverUtils.convertBean(skuList, OrderSpu.class);
            orderSpu.setOrderId(orderInfo.getId());
            orderSpu.setAfterSaleStatus(20);
            orderSpu.setItemCode(skuList.getSpuCode());
            orderSpu.setSkuCode(skuList.getSkuCode());
            orderSpu.setIsAfterSale(0);
            if (!CollectionUtils.isEmpty(skuList.getPhasePrices())) {
                skuList.getPhasePrices().forEach(phasePrice -> {
                    OrderPhase phase = BeanConverUtils.convertBean(phasePrice,OrderPhase.class);
                    phase.setStatus(BaseEnum.StatusEnum.DISABLE.getCode());
                    phase.setAmount(phasePrice.getAmount().multiply(new BigDecimal(orderSpu.getSkuQty())));
                    phase.setOrderNo(orderInfo.getId());
                    phase.setMasterOrderId(orderInfo.getMasterOrderId());
                    orderPhaseDao.insert(phase);
                });
                orderInfo.setPayType(OrderEnum.PayType.PHASE.getCode());
                orderInfo.setExpirationTime(skuList.getPhasePrices().get(0).getEndTime());
                orderInfo.setStartPayTime(skuList.getPhasePrices().get(0).getStartTime());
            }

            spus.add(orderSpu);
            orderSpuDao.insert(orderSpu);
        }
        return spus;
    }


}
